import java.util.Random;

public class PlusArray {
    private static long sum1;
    private static long sum2;

    public static void main(String[] args) {
        //long sum1,sum2;
        long[] array=new long[1000_0000];
        Random random=new Random();
        for (int i = 0; i < array.length; i++) {
            array[i]=random.nextInt(100)+1;
            //System.out.println(array[i]);
        }

        long beginT=System.currentTimeMillis();
        Thread t1=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("正在执行数组偶数下表和");
                for (int i = 0; i < array.length; i++) {
                    if(i%2==0){
                        sum1+=array[i];

                    }
                }
                //System.out.println(sum1);
            }

        },"线程1");

        Thread t2=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("正在执行数组奇数下表和");
                for (int i = 0; i < array.length; i++) {
                    if(i%2!=0){
                        sum2+=array[i];
                    }
                }
                //System.out.println(sum2);
            }
        },"线程2");
//        System.out.println(sum1);
//        System.out.println(sum2);
        //System.out.println("总和为"+(sum1+sum2));
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endT=System.currentTimeMillis();
        System.out.println("总和为"+(sum1+sum2));
        System.out.println("总耗时间为："+(endT-beginT));
    }
}
